package com.shop.loan.model.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import xyz.erupt.core.view.Page;

@ApiModel("返回结构")
@Data
public class ResResult {

    @ApiModelProperty(name = "msg", value = "名称", dataType = "string", example = "1")
    private String msg;

    @ApiModelProperty(name = "data", value = "数据", dataType = "string", example = "1")
    private Object data;

    @ApiModelProperty(name = "code", value = "状态码  1：成功", dataType = "int", example = "1")
    private Integer code;

    @ApiModelProperty(name = "token", value = "token", dataType = "string", example = "1")
    private String token;

    public ResResult() {
    }

    public ResResult(Page page) {
        JSONObject backJS = new JSONObject();
        backJS.put("data", JSON.parseArray(JSON.toJSONString(page.getList())));
        backJS.put("pages", page.getTotalPage());
        backJS.put("total", page.getTotal());

        this.data = backJS;
        this.code = Code.SUCCESS.getCode();
        this.msg = Code.SUCCESS.getMsg();
    }

    public ResResult(Integer code, String msg, Object data) {
        this.msg = msg;
        this.data = data;
        this.code = code;
    }

    public ResResult(Code code) {
        this.code = code.getCode();
        this.msg = code.getMsg();
    }

    public ResResult(Code code, Object data) {
        this.code = code.getCode();
        this.msg = code.getMsg();
        this.data = data;
    }

    public ResResult(Code code, Object data, String token) {
        this.code = code.getCode();
        this.msg = code.getMsg();
        this.data = data;
        this.token = token;
    }


    public ResResult(Object data, long pages, long total) {
        JSONObject backJS = new JSONObject();
        backJS.put("data", data);
        backJS.put("pages", pages);
        backJS.put("total", total);

        this.data = backJS;
        this.code = Code.SUCCESS.getCode();
        this.msg = Code.SUCCESS.getMsg();
    }

}
